package wangjg.dbfx;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

public class SessionFactory {
	
	private static ThreadLocal<JdbcSession> tl = new ThreadLocal<JdbcSession>();
	
	private DataSource dataSource;
	
	public JdbcSession getCurrentSession(){
		JdbcSession session = tl.get();
		if(session==null){
			session = new JdbcSession();
			tl.set(session);
		}
		try {
			Connection conn = dataSource.getConnection();
			session.setConnection(conn);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		return session;
	}


	public DataSource getDataSource() {
		return dataSource;
	}


	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
}
